# Importing the necessary libraries
import numpy as np
import pandas as pd
import datetime
import seaborn as sns
%matplotlib inline
from pandas_datareader import data, wb
import warnings
warnings.filterwarnings('ignore')
import matplotlib.pyplot as plt
sns.set_style('whitegrid')
import plotly
import cufflinks as cf
cf.go_offline()
# Setting the start dat to January 1, 2008
# Setting the end date to January 1 2018
start = datetime.datetime(2008,1,1)
end = datetime.datetime(2018,1,1)
# Importing the stock data from Yahoo Finance
# For the following Companies
# Apple
AAPL = data.DataReader("AAPL",'yahoo',start,end)
# Google or Alphabet Inc.
GOOGL = data.DataReader("GOOGL",'yahoo',start,end)
# Microsoft
MSFT = data.DataReader("MSFT",'yahoo',start,end)
# Amazon
AMZN = data.DataReader("AMZN",'yahoo',start,end)
# Creating a list of ticker symbols in alphabetical order.
tickers = ['AAPL','AMZN','GOOGL','MSFT']
# Concatanating the dataframes in a single dataframe.
stocks = pd.concat([AAPL, AMZN, GOOGL, MSFT], axis = 1, keys = tickers)
# Setting the column names
stocks.columns.names = ['Stock Ticker','Stock Info']
stocks.head()
# Getting the max Close price for each companies
stocks.xs(key = 'Close',axis = 1, level = 'Stock Info').max()
# Getting the returns for each company's stock
returns = pd.DataFrame()
for i in tickers:
returns[i + ' Returns'] = stocks[i]['Close'].pct_change()
returns.head()
# Plotting the pairplot of the returns DataFrame
# The the first value is NaN, plotting the data from the 2nd index.
sns.pairplot(returns[1:])
# Finding when each company had the worst single day return
returns.idxmin()
# Finding when each company had the biggest single day gain
returns.idxmax()
# 2008 and 2009 are the years where the companies had their highest and lowest returns in single days
# Calculating the standard deviation of the stocks over the course of 10 years
returns.std()
# Creating a line plot to show the changes in stock price over time
for x in tickers:
stocks[x]['Close'].plot(label = x, figsize = (15,7))
plt.legend()
# Comparison of stocks over the period using plotly.
stocks.xs(key='Close',axis=1,level='Stock Info').iplot()
# 10 year moving average against the actual closing price
plt.figure(figsize=(15,5))
AAPL['Close'].ix['2008-01-01':'2018-01-01'].rolling(window=30).mean().plot(label = 'Moving Average')
AAPL['Close'].ix['2008-01-01':'2018-01-01'].plot(label='Close')
plt.legend()
# Correlation between the closing prices of the stocks
sns.heatmap(stocks.xs(key='Close',axis=1,level = 'Stock Info').corr(),annot=True)
# Clustermap to find what stocks corelate to each other
# From the results of the map, we can see that stocks of Google and Microsoft correlate to each other while Apple
# and Amazon correlate to each other.
sns.clustermap(stocks.xs(key='Close',axis=1,level = 'Stock Info').corr(),annot=True)
# Apple had the lowest and highest return value in the span of 15 days.
# Using Candle maps to find out how Apple's stock changed over the two months.
aapl0809 = AAPL[['Open','High','Low','Close']].ix['2008-09-01':'2008-11-01']
aapl0809.iplot(kind='candle')
# Amazon had the highest increase in their stock price. Creating a simple moving averages plot
AMZN['Close'].ta_plot(study='sma',periods=[13,21,55])
# Google's stock also increased to a great extent over the course of 10 years
# Creating a Bollinger Band Plot to see how the upper, lower, sma faired with the closing price.
GOOGL['Close'].ta_plot(study='boll')